package vn.timsua.utils;

import java.util.Collection;
import java.util.Vector;
import java.sql.Timestamp;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2011</p>
 *
 * <p>Company: timtsua.vn</p>
 * @author Nguyen Manh Hung
 * @version 1.0
 */
public class Tools {
    public Tools() {
    }

    public static String buildAsciiName(String sAsciiName) {
        sAsciiName = sAsciiName.replaceAll(" ", "");
        sAsciiName = sAsciiName.replaceAll("\\?", "");
        sAsciiName = sAsciiName.replaceAll("%", "");
        sAsciiName = sAsciiName.replaceAll("=", "");
        sAsciiName = sAsciiName.replaceAll("\"", "");
        sAsciiName = sAsciiName.replaceAll("'", "");
        sAsciiName = sAsciiName.replaceAll("@", "");
        sAsciiName = sAsciiName.replaceAll("\\.", "");
        sAsciiName = sAsciiName.replaceAll("#", "");
        if (sAsciiName.length() > 10) {
            System.out.println(sAsciiName.length());
            sAsciiName = sAsciiName.substring(0, 9);
        }
        return sAsciiName;
    }

    public static String getPartition(String tablename,Timestamp ts) {
        int month = DateProc.getMonth(ts);
        String sMonth=String.valueOf(month);
        if(sMonth.length()==1) sMonth="0"+sMonth;
        int year = DateProc.getYear(ts);
        return tablename+"_P"+String.valueOf(year)+sMonth;
    }

    public static String getMemberPartition(String sUserName) {
        if ((sUserName == null) || ("".equals(sUserName)))
            return null;
        String sPartitionName = null;
        char cFirst = sUserName.toLowerCase().charAt(0);
        if (sUserName.length() > 1) {
            if(cFirst < 'a'){
                sPartitionName = "MEMBER_PA";
            }else if((cFirst >= 'a')&&(cFirst < 'y')) {
                char cPartition = (char) (((cFirst - 'a' + 2) / 2 * 2) + 'a');
                sPartitionName="MEMBER_P"+String.valueOf(cPartition);
            } else {
                sPartitionName = "MEMBER_P9";
            }
        } else {
            if(cFirst <= 'a'){
                sPartitionName = "MEMBER_PA";
            }else if((cFirst > 'a')&&(cFirst <= 'y')) {
                char cPartition = (char) (((cFirst - 'a' + 1) / 2 * 2) + 'a');
                sPartitionName="MEMBER_P"+String.valueOf(cPartition);
            } else {
                sPartitionName = "MEMBER_P9";
            }
        }
        return sPartitionName;
    }

    /*
     * In
     *   text: a string having some seperator(s)
     * Out
     *   a collection of elements without (between) seperator
     */
    /*================================================================*/
    public static Collection parseString(String text, String seperator) {
        Vector vResult = new Vector();
        if (text == null || "".equals(text))
            return vResult;
        String tempStr = text.trim();
        String currentLabel = null;
        int index = tempStr.indexOf(seperator);
        while (index != -1) {
            currentLabel = tempStr.substring(0, index).trim();
            //Only accept not null element
            if (!"".equals(currentLabel))
                vResult.addElement(currentLabel);
            tempStr = tempStr.substring(index + 1);
            index = tempStr.indexOf(seperator);
        }
        //Last label
        currentLabel = tempStr.trim();
        if (!"".equals(currentLabel))
            vResult.addElement(currentLabel);
        return vResult;
    }

    public static String MimeType2Ext(String sMimeType) {
        if ((sMimeType == null)||("".compareTo(sMimeType)==0)) {
            return "";
        }
        if(sMimeType.compareToIgnoreCase("image/gif")==0){
            return ".gif";
        }else if((sMimeType.compareToIgnoreCase("image/jpeg")==0)||(sMimeType.compareToIgnoreCase("image/pjpeg")==0)){
            return ".jpg";
        }else if(sMimeType.compareToIgnoreCase("audio/amr")==0){
            return ".amr";
        }else if(sMimeType.compareToIgnoreCase("audio/mmf")==0){
            return ".mmf";
        }else if(sMimeType.compareToIgnoreCase("audio/mid")==0){
            return ".mid";
        }else if(sMimeType.compareToIgnoreCase("video/3gpp")==0){
            return ".3gp";
        }else if(sMimeType.compareToIgnoreCase("video/mpeg")==0){
            return ".mp4";
        }else if(sMimeType.compareToIgnoreCase("text/jad")==0){
            return ".jad";
        }else if(sMimeType.compareToIgnoreCase("application/jar")==0){
            return ".jar";
        }

        return ".unk";
    }

    public static String getMobileType(String sUserAgent) {
        String sRes="";
        if ((sUserAgent == null)||("".compareTo(sUserAgent)==0)) {
            return sRes;
        }
        int iFlash=sUserAgent.indexOf("/");
        if(iFlash>0) sRes=sUserAgent.substring(0,iFlash);
        sRes=sRes.toUpperCase();
        if(sRes.startsWith("NOKIA")){
            sRes=sRes.replaceFirst("NOKIA","N");
        }
        return sRes;
    }

    public static boolean isNumeric(String str) {
        try {
            //Long.parseLong(str);
            Integer.parseInt(str);
        } catch (NumberFormatException nex) {
            return false;
        } catch (NullPointerException nPex) {
            return false;
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    public static boolean isNumeric2(String sNumber) {
        if (sNumber == null || "".equals(sNumber)) {
            return false;
        }
        for (int i = 0; i < sNumber.length(); i++) {
            char ch = sNumber.charAt(i);
            char ch_max = (char) 0x39;
            char ch_min = (char) 0x30;
            if ((ch < ch_min) || (ch > ch_max)) {
                return false;
            }
        }
        return true;
    }

    public static String getString(String sAdd,int iLen) {
        String sOut="";
        if ((sAdd != null) && (!"".equals(sAdd)))
            for (int i = 0; i < iLen; i++)
                sOut += sAdd;
        return sOut;
    }


    private boolean isAlphaNumeric(final String s) {
        return s.matches("\\p{Alnum}+");
    }

    private boolean isAlphaNumeric2(final String s) {
        final char[] chars = s.toCharArray();
        for (int x = 0; x < chars.length; x++) {
            final char c = chars[x];
            if ((c >= 'a') && (c <= 'z')) continue; // lowercase
            if ((c >= 'A') && (c <= 'Z')) continue; // uppercase
            if ((c >= '0') && (c <= '9')) continue; // numeric
            return false;
        }
        return true;
    }

    // AccountId = 998{000}MemberId (length=10)
    public static String buildAccountId(String sMemberId) {
        if (sMemberId == null || "".equals(sMemberId) || sMemberId.length() > 10) {
            System.out.println("Tool.buildAccountId: invalid MemberId " + sMemberId);
            return null;
        }
        String sAccountId = "998";
        for (int i = 0; i < (7 - sMemberId.length()); i++) {
            sAccountId = sAccountId + "0";
        }
        return (sAccountId + sMemberId);

    }

    /**
     *
     * @param sAdd String
     * @param iLen int
     * @return String
     * Last modified:01/04/2008 by Lu Ngoc Nhan.
     */
    public static String expanseString(String sAdd,int iLen) {
        String sOut="";
        if ((sAdd != null) && (!"".equals(sAdd)))
            for (int i = 0; i < iLen; i++)
                sOut += sAdd;
        return sOut;
    }

    public static String replace(String str, String pattern, String replace) {
    int s = 0;
    int e = 0;
    StringBuffer result = new StringBuffer();

    while ((e = str.indexOf(pattern, s)) >= 0) {
        result.append(str.substring(s, e));
        result.append(replace);
        s = e+pattern.length();
    }
    result.append(str.substring(s));
    return result.toString();
}


    public static void main(String args[]){
        //String s="Nokia3220/2.0 (04.80) Profile/MIDP-2.0 Configuration/CLDC-1.1";
        //System.out.println(getMobileType(s));
        //System.out.println(Tools.isNumeric("123"));
        //String s="";
        //System.out.println(s+"a");
        //System.out.println(ConvertData.convertUni2ASCII("ân's & ăn"));
        System.out.println(expanseString("aa",5));
    }
}
